<!-- 首页 -->
<template>
	<view>
		<view>
		    <view class="user-box" v-for="(item, index) in dataList" :key="index">
		    	<u-avatar @click="chat(item)" size="large" :src="item.avatar"></u-avatar>
		    	<view class="user-box-right">
		    		<view>
		    			<view class="user-name-box">
		    				<view class="nickname">{{item.nickName}}</view>
		    				<view class="sex-box-nv" v-if="item.sex == 1">
		    					<u-icon name="ziyuan2" custom-prefix="iconfont"></u-icon>
		    					<view class="age-text">{{item.age}}</view>
		    				</view>
		    				<view class="sex-box" v-else>
		    					<u-icon name="ziyuan3" custom-prefix="iconfont"></u-icon>
		    					<view class="age-text">{{item.age}}</view>
		    				</view>
							<view class="vip-box" v-if="item.isPay == '1'">
								<u-icon name="xianxinghuiyuan" custom-prefix="iconfont"></u-icon>
								<view class="vip-text">贵{{item.coinLevel}}</view>
							</view>
		    			</view>
		    			<view class="user-tag-box">
		    				<view class="user-tag">{{item.city}}</view>
		    				<view class="line"></view>
		    				<view class="user-tag">{{item.distance}}km</view>
		    			</view>
						<view v-if="item.albums">
							<u-album :urls="item.albums.split(',')" :multipleSize="80" :rowCount="3" :maxCount="3" :maxRow="1"></u-album>
						</view>
						<view v-else>
							<view v-if="item.signName == '' || item.signName == null ">{{item.signName}}</view>
							<view class="sign-name" v-else>{{item.signName}}</view>
						</view>
		    		</view>
		    		<view>
						<u-button size="mini" @click="addWechat(item)" v-if="item.wechatFlag">
							<u-icon name="weixin-fill" color="#19be6b" size="36"></u-icon>
						</u-button>
		    			<u-button size="mini" @click="apply(item)" v-else>
							<u-icon name="chat-fill" color="#1296db" size="36"></u-icon>
						</u-button>
		    		</view>
		    	</view>
		    </view>
			
			<view class="loadmore-box" v-if="dataList.length > 0">
				<u-loadmore :status="status" @loadmore="load"></u-loadmore>
			</view>
			<u-back-top :scrollTop="scrollTop"></u-back-top>
		</view>
		<u-toast ref="uToast" />
	</view>
</template>

<script>
import {mapState} from 'vuex';
export default {
	props:{
		scrollTop: { // 当前tab的下标 (除了支付宝小程序必须在这里定义, 其他平台都可不用写, 因为已在MescrollMoreItemMixin定义)
			type: Number,
			default(){
				return 0
			}
		},
	},
	data() {
		return {
			status: 'loadmore',
			scrollTop: 0,
			isEnd: false,
			current: 1,
			pageSize: 20,
			dataList: [],
		};
	},
	created() {
		this.query();
	},
	computed: {
		...mapState(['vuex_query']),
	},
	methods: {
		query() {
			this.current = 1;
			this.isEnd = false;
			this.status = 'loadmore';
			this.loadquery();
		},
		loadquery() {
			this.$u.get('/system/user/getList',{
				pageNum: this.current,
				min: this.vuex_query.min,
				max: this.vuex_query.max,
				pageSize: this.pageSize,
			}).then(res => {
				var totalPage = Math.ceil(res.total/this.pageSize);
				this.pages = totalPage;
				this.current ++;
				this.status = 'loadmore';
				if(this.current > this.pages){
					this.status = 'nomore';
					this.isEnd = true;
				}
				
				this.dataList = res.rows;
			}).catch(err => {
				
			})
		},
		load() {
			this.loadmore();
		},
		loadmore() {
			this.status = 'loading';
			if(this.isEnd){
				this.status = 'nomore';
				return;
			}
			this.$u.get('/system/user/getList',{
				pageNum: this.current,
				min: this.vuex_query.min,
				max: this.vuex_query.max,
				pageSize: this.pageSize,
			}).then(res => {
				var totalPage = Math.ceil(res.total/this.pageSize);
				this.pages = totalPage;
				this.current ++;
				this.status = 'loadmore';
				if(this.current > this.pages){
					this.status = 'nomore';
					this.isEnd = true;
				}
				
				this.dataList = this.dataList.concat(res.rows);
			}).catch(err => {
				
			})
		},
		chat(item) {
			this.judgeLogin(() => {
				this.$u.route({
					url: 'packgeMine/pages/center/index',
					params: {
						userId: item.userId,
					}
				});
			});
		},
		apply(e) {
			this.judgeLogin((rs) => {
				if(rs.user.phonenumber == null || rs.user.phonenumber == ''){
					this.$store.commit('setPhone', true);
					return;
				}
				
				this.$u.post('/wq/friendApply/add',{
					userId: e.userId,
				}).then(res => {
					var roomId = res.data.roomId;
					this.$u.route({
						url: 'packgeMine/pages/chat/chat',
						params: {
							roomId: roomId,
						}
					});
				});
				
			});
		},
		getAmount() {
			this.$u.get('/pay/userCoin/getAmount').then(res => {
				this.$store.commit('setAmount', res.data);
			});
		},
		addApply(e) {
			this.$u.post('/wq/friendApply/addWechat',{
				userId: e.userId,
			}).then(res => {
				this.getAmount();
				this.$u.route({
					url: 'packgeMine/pages/chat/chat',
					params: {
						roomId: res.data.roomId,
					}
				});
			});
		},
		addVipApply(e) {
			this.$u.post('/wq/friendApply/addVipWechat',{
				userId: e.userId,
			}).then(res => {
				this.$refs.uToast.show({
					title: '剩余'+res.wechatNum+'次',
					type: 'success',
					duration: 1000,
					url: 'packgeMine/pages/chat/chat?roomId='+res.roomId
				});
			});
		},
		addWechat(item) {
			this.judgeVipNum((res) => {
				if(res.wechatNum > 0){
					this.addVipApply(item);
				}else{
					this.addApply(item);
				}
			});
		},
		subscribeMessage(tmplIds) {
			uni.requestSubscribeMessage({
				//数组，最多放三个订阅模板的ID
				tmplIds: tmplIds,
				success: function(res) {
					console.log(JSON.stringify(res));
				},
				fail: function() {
		
				},
				complete: (res) => {
					console.log(333,res)
				}
			});
		},
	}
};
</script>
<style lang="scss" scoped>
	page {
		background-color: #fff;
	}
	.user-box {
		display: flex;
		align-items: center;
		padding: 15px;
		padding-bottom: 0;
	}
	.user-name-box {
		display: flex;
		align-items: center;
	}
	.nickname {
		font-size: 28rpx;
		margin-right: 5px;
		overflow: hidden;
		white-space: nowrap;
		text-overflow: ellipsis;
		max-width: 100px;
	}
	.sex-box {
		background-color: #2b85e4;
		display: flex;
		color: #fff;
		padding: 4rpx 10rpx;
		border-radius: 20px;
		font-size: 16rpx;
	}
	.sex-box-nv {
		background-color: #fe709f;
		display: flex;
		color: #fff;
		padding: 4rpx 10rpx;
		border-radius: 20px;
		font-size: 16rpx;
	}
	.user-tag-box {
		display: flex;
		align-items: center;
	}
	.user-tag {
		font-size: 20rpx;
		color: #82848a;
		margin: 10rpx 0;
	}
	.user-box-right {
		margin-left: 15px;
		display: flex;
		align-items: center;
		justify-content: space-between;
		flex: 1;
	}
	.sign-name {
		font-size: 24rpx;
		color: #8d8d8d;
		text-overflow: ellipsis;
		width: 400rpx;
		overflow: hidden;
		white-space: nowrap;
	}
	.renzheng-box {
		display: flex;
		align-items: center;
		margin-right: 5px;
	}
	.line {
		float:left;
		width: 1px;
		height: 16rpx; 
		background: #e5e5e5;
		margin: 0 5px;
	}
	.vip-box {
		color: #fff;
		margin-left: 5px;
		font-size: 20rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		background-color: #f7ce05;
		padding: 4rpx 8rpx;
		border-radius: 20px;
	}
	.vip-text {
		font-size: 16rpx;
		line-height: 2px;
	}
	.loadmore-box {
		padding: 15px 0;
		padding-bottom: 140rpx;
	}
</style>
